package cn.com.aiton.educenter.service.impl;

import cn.com.aiton.educenter.entity.PestsControl;
import cn.com.aiton.educenter.entity.PestsTrap;
import cn.com.aiton.educenter.entity.vo.PestsQrcodeVo;
import cn.hutool.core.date.DateUtil;
import com.alibaba.excel.EasyExcel;
import cn.com.aiton.educenter.entity.excel.PestsControlExcel;
import cn.com.aiton.educenter.mapper.PestsControlMapper;
import cn.com.aiton.educenter.service.PestsControlService;
import cn.com.aiton.educenter.utils.FileUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.joda.time.LocalDateTime;
import org.junit.Test;
import org.springframework.stereotype.Service;

import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 防治业务数据表 服务实现类
 * </p>
 *
 * @author testAuthor
 * @since 2021-01-29
 */
@Service
public class PestsControlServiceImpl extends ServiceImpl<PestsControlMapper, PestsControl> implements PestsControlService {
    /**
     * 最简单的写
     * <p>1. 创建excel对应的实体对象 参照{@link PestsControlExcel}
     * <p>2. 直接写即可
     */
    @Test
    public void simpleWrite() {
        // 写法1
        String fileName = FileUtil.getPath() + "simpleWrite" + System.currentTimeMillis() + ".xlsx";
        // 这里 需要指定写用哪个class去写，然后写到第一个sheet，名字为模板 然后文件流会自动关闭
        // 如果这里想使用03 则 传入excelType参数即可
        EasyExcel.write(fileName, PestsControlExcel.class).sheet("模板").doWrite(this.list(null));

        // 写法2
        //fileName = FileUtil.getPath() + "simpleWrite" + System.currentTimeMillis() + ".xlsx";
        // 这里 需要指定写用哪个class去写
//        ExcelWriter excelWriter = null;
//        try {
//            excelWriter = EasyExcel.write(fileName, PestsControlExcel.class).build();
//            WriteSheet writeSheet = EasyExcel.writerSheet("模板").build();
//            excelWriter.write(data(), writeSheet);
//        } finally {
//            // 千万别忘记finish 会帮忙关闭流
//            if (excelWriter != null) {
//                excelWriter.finish();
//            }
//        }
    }

    /**
     * 根据参数只导出指定列
     * <p>
     * 1. 创建excel对应的实体对象 参照{@link PestsControlExcel}
     * <p>
     * 2. 根据自己或者排除自己需要的列
     * <p>
     * 3. 直接写即可
     *
     * @since 2.1.1
     */
    @Test
    public void excludeOrIncludeWrite() {
        String fileName = FileUtil.getPath() + "excludeOrIncludeWrite" + System.currentTimeMillis() + ".xlsx";

        // 根据用户传入字段 假设我们要忽略 date
//        Set<String> excludeColumnFiledNames = new HashSet<String>();
//        excludeColumnFiledNames.add("date");
//        // 这里 需要指定写用哪个class去写，然后写到第一个sheet，名字为模板 然后文件流会自动关闭
//        EasyExcel.write(fileName, PestsControlExcel.class).excludeColumnFiledNames(excludeColumnFiledNames).sheet("模板")
//                .doWrite(data());

//        fileName = FileUtil.getPath() + "excludeOrIncludeWrite" + System.currentTimeMillis() + ".xlsx";
        // 根据用户传入字段 假设我们只要导出 date
        Set<String> includeColumnFiledNames = new HashSet<String>();
        includeColumnFiledNames.add("date");
        // 这里 需要指定写用哪个class去写，然后写到第一个sheet，名字为模板 然后文件流会自动关闭
        EasyExcel.write(fileName, PestsControlExcel.class).includeColumnFiledNames(includeColumnFiledNames).sheet("模板")
                .doWrite(this.list(null));
    }

    /**
     * 年月日时间处理
     * @param time
     * @return
     */
    public static String parse_yyyyMMdd(Date time) {
        return DateUtil.format(time, "yyyy-MM-dd");
    }

    /**
     * 年月日时间处理
     * @param date
     * @return
     */
    public static String parse_yyyyMM(Date date) {

        return DateUtil.format(date, "yyyy-MM");
    }

    /**
     * 折线图统计(按月)
     * <P>
     *     1- 患者统计
     *     2- 出货统计
     * </P>
     * @param
     * @return
     */
    public List<Map<String,Integer>> getStatistics(List<PestsControl> pestsControls) {
        if (pestsControls.size() != 0) {
            Map<String, List<PestsControl>> groupByMonth = pestsControls.stream().collect(
                    Collectors.groupingBy(o -> parse_yyyyMMdd(o.getStime()))
            );
            List<Map<String,Integer>> patientList = new ArrayList<>();
            groupByMonth.forEach((k, v) -> {
                Map<String,Integer> ma = new HashMap<>();
                ma.put(k, v.size());
                patientList.add(ma);
            });
            return patientList;
        }
        return null;
    }
    //查询某一天注册人数
    @Override
    public Integer countDay(String day) {
        return baseMapper.countPestsDay(day);
    }

    @Override
    public Integer countDayByProjectId(String day, String projectId) {
        return baseMapper.countPestsDayByProjectId(day,projectId);
    }

    @Override
    public Integer countDayByProjectIdAndMemberId(String day, String projectId, String memberId) {
        return baseMapper.countPestsDayByProjectIdAndMemberId(day,projectId,memberId);
    }

    @Override
    public List<PestsQrcodeVo> getPestsControlAndQrcode(String userId) {
        List<PestsQrcodeVo> cc = baseMapper.getPestsControlAndQrcode(userId);
        return cc;
    }

    @Override
    public IPage<PestsQrcodeVo> pagePestsControlAndQrcode(Page<PestsQrcodeVo> page, Wrapper wrapper) {
        IPage<PestsQrcodeVo> cc = baseMapper.pagePestsControlAndQrcode(page,wrapper);
        return cc;
    }

    @Override
    public Integer countPestsByProjectId(String projectId) {
        QueryWrapper<PestsControl> wrapper = new QueryWrapper<>();
        wrapper.eq("project_id",projectId);
        Integer count =  baseMapper.selectCount(wrapper);
        return count;
    }

}
